import ecfsConfig from '../../../config/ecfs.config.js'
import { merge } from '../../../utils/util.js'
/**
 * [handleCaseList 处理案件列表]
 * @param  {[page]}     page         [page组件实例]
 * @param  {[JSON]}    data        [服务端返回的数据]
 */
export function handleCaseList(page, data) {
  if (data.code === 200) {
    if (data.body.resultcount > 0) {
      let copyBody = merge({}, data.body),
        result = copyBody.result
      Object.keys(result).forEach(outerKey => {
        let parties = result[outerKey].party
        if (result[outerKey].case.judge != null) {
          if (result[outerKey].case.judge.length > 10) {
            result[outerKey].case.judgeClippedName = result[outerKey].case.judge.substring(0, 10) + '...'
          } else {
            result[outerKey].case.judgeClippedName = result[outerKey].case.judge
          }
        } else {
          result[outerKey].case.judgeClippedName = ''
        }
        let accusers = [],
          appellees = [],
          thirds = [],
          firstAccuserName = '',
          firstAppelleeName = ''
        if (parties.length > 0) {
          Object.keys(parties).forEach(innerKey => {
            if (parties[innerKey].name.length > 10) {
              parties[innerKey].partyClippedName = parties[innerKey].name.substring(0, 10) + '...'
            } else {
              parties[innerKey].partyClippedName = parties[innerKey].name
            }

            if (parties[innerKey].litigationStatus === 1) {
              accusers.push(parties[innerKey])
            } else if (parties[innerKey].litigationStatus === 2) {
              appellees.push(parties[innerKey])
            }
          })

          if (accusers.length === 0) {
            firstAccuserName = ''
          } else if (accusers.length === 1) {
            firstAccuserName = accusers[0].partyClippedName
          } else {
            sortParties(accusers)
            firstAccuserName = accusers[0].partyClippedName
          }

          if (appellees.length === 0) {
            firstAppelleeName = ''
          } else if (appellees.length === 1) {
            firstAppelleeName = appellees[0].partyClippedName
          } else {
            sortParties(appellees)
            firstAppelleeName = appellees[0].partyClippedName
          }
        }
        let caseInfo = result[outerKey].case
        let date = new Date(caseInfo.gmtModified),
          Y = date.getFullYear() + '-',
          M = (date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1) + '-',
          D = date.getDate() + ' '
        caseInfo.formatDate = Y + M + D

        result[outerKey].firstAccuserName = firstAccuserName
        result[outerKey].firstAppelleeName = firstAppelleeName
        
      })
      if (page.data.pageNum === 1) {
        page.setData({
          caseList: result,
          backEndStatus: 0,
          loading: false
        })
      } else {
        page.setData({
          caseList: page.data.caseList.concat(result),
          backEndStatus: 0,
          loading: false
        })
      }
    } else {
      page.setData({
        caseList: [],
        backEndStatus: 1,
        loading: false
      })
    }
    if (page.data.pageSize > page.data.caseList.length) {
      page.data.pageNum = page.data.pageNum === 1 ? 1 : page.data.pageNum - 1
    }else {
      page.data.pageNum += 1
    }
   
  } else if (data.code === 205403) {
    page.setData({
      caseList: [],
      backEndStatus: 1,
      loading: false
    })
  }
}

/**
 * [handleCaseInfo 处理案件详情]
 * @param  {[page]}     page         [page组件实例]
 * @param  {[JSON]}     data        [服务端返回的数据]
 */
export function handleCaseInfo(page, data) {
  if (data.code === 200) {
    let copyResult = merge({}, data.body)
    let shorcut_3 = 3
    let shorcut_10 = 10
    const { caseType, caseTypeName, id: caseId } = copyResult.case
    copyResult.accusers = []
    copyResult.appellees = []
    copyResult.thirds = []

    copyResult.fileLocation = ecfsConfig.IS_PRODUCTION ? (ecfsConfig.SERVER_DOMAIN + ecfsConfig.PICTURE_PREVIEW_URL) : (ecfsConfig.SERVER_DOMAIN + ecfsConfig.RAP_URL_PREFIX + ecfsConfig.PICTURE_PREVIEW_URL)

    if (copyResult.party !== null && copyResult.party.length > 0) {
      Object.keys(copyResult.party).forEach(outerKey => {
        let party = copyResult.party[outerKey]
        const { id: partyId, name: partyName } = party
        party.caseId = caseId
        party.caesType = caseType
        party.caseTypeName = caseTypeName
        party.agentIds = []
        party.fileList = []
        //当事人证件照按照正、反排序
        sortCertificates(party.certificates)
        Object.keys(party.certificates).forEach(key2 => {
          let certificate = party.certificates[key2]
          let fileUrl = copyResult.fileLocation + certificate.newName
          party.fileList.push(fileUrl)
        })
        if (party.name.length > shorcut_10) {
          party['clippedName_' + shorcut_10] = party.name.substring(0, 10) + '...'
        } else {
          party['clippedName_' + shorcut_10] = party.name
        }
        if ((party.address + party.addressDetail).length > shorcut_3) {
          party['clippedAddressDetail_' + shorcut_3] = (party.address + party.addressDetail).substring(0, 3) + '...'
        } else {
          party['clippedAddressDetail_' + shorcut_3] = (party.address + party.addressDetail)
        }
        if ((party.address + party.addressDetail).length > shorcut_10) {
          party['clippedAddressDetail_' + shorcut_10] = (party.address + party.addressDetail).substring(0, 10) + '...'
        } else {
          party['clippedAddressDetail_' + shorcut_10] = (party.address + party.addressDetail)
        }
        if ((party.homeRegister + party.homeRegisterDetail).length > shorcut_3) {
          party['clippedHomeRegisterDetail_' + shorcut_3] = (party.homeRegister + party.homeRegisterDetail).substring(0, 3) + '...'
        } else {
          party['clippedHomeRegisterDetail_' + shorcut_3] = (party.homeRegister + party.homeRegisterDetail)
        }
        if ((party.homeRegister + party.homeRegisterDetail).length > shorcut_10) {
          party['clippedHomeRegisterDetail_' + shorcut_10] = (party.homeRegister + party.homeRegisterDetail).substring(0, 10) + '...'
        } else {
          party['clippedHomeRegisterDetail_' + shorcut_10] = (party.homeRegister + party.homeRegisterDetail)
        }
        if ((party.unitAddress + party.unitAddressDetail).length > shorcut_3) {
          party['clippedUnitAddressDetail_' + shorcut_3] = (party.unitAddress + party.unitAddressDetail).substring(0, 3) + '...'
        } else {
          party['clippedUnitAddressDetail_' + shorcut_3] = (party.unitAddress + party.unitAddressDetail)
        }
        if ((party.unitAddress + party.unitAddressDetail).length > shorcut_10) {
          party['clippedUnitAddressDetail_' + shorcut_10] = (party.unitAddress + party.unitAddressDetail).substring(0, 10) + '...'
        } else {
          party['clippedUnitAddressDetail_' + shorcut_10] = (party.unitAddress + party.unitAddressDetail)
        }
        if (party.unitName.length > shorcut_3) {
          party['clippedUnitName_' + shorcut_3] = party.unitName.substring(0, 10) + '...'
        } else {
          party['clippedUnitName_' + shorcut_3] = party.unitName
        }
        if (party.unitName.length > shorcut_10) {
          party['clippedUnitName_' + shorcut_10] = party.unitName.substring(0, 10) + '...'
        } else {
          party['clippedUnitName_' + shorcut_10] = party.unitName
        }
        if (party.litigationStatus && party.litigationStatus === 1) {
          switch (party.gender) {
            case 1:
              party.genderName = '男'
              break
            case 2:
              party.genderName = '女'
              break
            default:
              party.genderName = ''
              break
          }
        }
        switch (party.litigationStatus) {
          case 1: {
            //if (party.agentIds === undefined) party.agentIds = []
            copyResult.accusers.push(party)
            break;
          }
          case 2: {
            copyResult.appellees.push(party)
            break;
          }
          case 3: {
            copyResult.thirds.push(party)
            break;
          }
        }


        if (copyResult.agent !== null && copyResult.agent.length > 0) {

          outer: for (let i = 0; i < copyResult.agent.length; i++) {
            let agent = copyResult.agent[i]
            agent.flag = 2
            agent.caseId = caseId
            if (agent.name.lenght > shorcut_10) {
              agent['clippedName_' + shorcut_10] = agent.name.substring(0, 10) + '...'
            } else {
              agent['clippedName_' + shorcut_10] = agent.name
            }
            inner: for (let j = 0; j < agent.partyIds.length; j++) {
              let _partyId = agent.partyIds[j].partyId
              delete agent.partyIds[j].id
              delete agent.partyIds[j].agentId
              if (partyId === _partyId) {
                party.agentIds.push(agent.id)
                agent.partyIds[j].partyName = partyName
                break inner
              }
            }
          }

        }
      })
      //console.log(copyResult.agent)
      for (let k = 0; k < copyResult.agent.length; k++) {
        let agent = copyResult.agent[k]
        let partyNames = []
        for (let l = 0; l < agent.partyIds.length; l++) {
          let partyName = l + 1 + '. ' + agent.partyIds[l].partyName
          partyNames.push(partyName)
        }
        agent.agentedNames = partyNames.join(' ')
        if (agent.agentedNames.length > shorcut_10) {
          agent['agentedNames_' + shorcut_10] = agent.agentedNames.substring(0,10) + '...'
        }else {
          agent['agentedNames_' + shorcut_10] = agent.agentedNames
        }

      }
    }
    //console.log(wx)
    //console.log(JSON.stringify(copyResult))
    wx.setStorage({
      key: 'caseInfo',
      data: copyResult,
    })
  }
}
/**
 * [sortParties 给当事人集合排序(按照名字长度)]
 * @param  {[Array]} parties [当事人集合]
 */
export function sortParties(parties) {
  for (let i = 0; i < parties.length - 1; i++) {
    let min = i
    for (let j = i + 1; j < parties.length; j++) {
      if (parties[min].name.length > parties[j].name.length) {
        min = j
      }
    }
    if (min != i) {
      let temp = parties[i]
      parties[i] = parties[min]
      parties[min] = temp
    }
  }
}

/**
 * [sortCertificates 证件照排序（按照正、反、手持----front字段）]
 * @param  {[Array]} certificates [证件照集合]
 */
export function sortCertificates(certificates) {
  for (let i = 0; i < certificates.length - 1; i++) {
    let min = i
    for (let j = i + 1; j < certificates.length; j++) {
      if (certificates[min].front > certificates[j].front) {
        min = j
      }
    }
    if (min != i) {
      let temp = certificates[i]
      certificates[i] = certificates[min]
      certificates[min] = temp
    }
  }
}

